<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

use Illuminate\Support\Str;

Route::get('/', function () {
    return view('welcome');
});

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::any("proxy/load", function () {
    $controller = toControllerName("proxy");
    return App::make($controller)->load();
});


Route::group(['middleware' => ['weixin']], function () {
    Route::any("remote/{companyId}/{method}", function ($companyId, $method) {
        $controller = toControllerName("remote");
        return App::make($controller)->$method($companyId);
    });

    Route::any("open/{appid}/{method}", function ($appid, $method) {
        $controller = toControllerName("open");
        return App::make($controller)->$method($appid);
    });
});

Route::group(['middleware' => ['log']], function () {
    Route::any("{controller}/{method}", function ($controller, $method) {
        $controller = toControllerName($controller);
        return App::make($controller)->$method();
    });
});

Route::any("{controller}/find/{id}", function ($controller, $id) {
    $controller = toControllerName($controller);
    return App::make($controller)->find($id);
});

function toControllerName($controller)
{
    $controller = ucfirst(Str::camel($controller));
    $controller = 'App\\Http\\Controllers\\' . $controller . 'Controller';
    return $controller;
}